package com.zretc.controller;

import com.github.pagehelper.PageInfo;
import com.zretc.entity.Auditoriums;
import com.zretc.service.AuditoriumsService;
import com.zretc.util.ExcelImportUtil;
import com.zretc.util.ExcelUtil;
import jdk.internal.util.xml.impl.Input;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("back")
public class BackAuditoriumsController {
    @Autowired
    private AuditoriumsService auditoriumsService;
    private static final Logger LOGGER = LoggerFactory.getLogger(BackAuditoriumsController.class);
    // 添加影厅
    @PostMapping("auditoriums")
    @ResponseBody
    public boolean saveAuditoriums(Auditoriums auditoriums){
        return auditoriumsService.insert(auditoriums);
    }
    // 删除影厅
    @DeleteMapping("auditoriums/{id}")
    @ResponseBody
    public boolean deleteAuditoriums(@PathVariable("id")String auditoriumsId){
        return auditoriumsService.delete(Integer.valueOf(auditoriumsId));
    }
    // 修改影厅
    @PutMapping("auditoriums")
    @ResponseBody
    public boolean updateAuditoriums(Auditoriums auditoriums){
        return auditoriumsService.update(auditoriums);
    }
    // 查询影厅
    @GetMapping("auditoriums")
    @ResponseBody
    public PageInfo<Auditoriums> getAuditoriumsList(String auditoriumsName,
                                                    String page,
                                                    String limit){
        // 组装条件
        Map<String,String> condition = new HashMap<>();
        condition.put("auditoriumsName",auditoriumsName);
        condition.put("pageNum",page);
        condition.put("pageSize",limit);
        return auditoriumsService.getAuditoriumsList(condition);
    }

    // 批量删除
    @DeleteMapping("auditoriums")
    @ResponseBody
    public boolean deleteAuditoriums(@RequestBody ArrayList<String> auditoriumsList){
        return auditoriumsService.deleteAuditoriumsList(auditoriumsList);
    }

    // 导出
    @GetMapping("auditoriums/export")
    public void exportAuditoriums(HttpServletResponse response){
        HSSFWorkbook export = auditoriumsService.export();
        // 文件名
        String fileName = "影厅数据";
        try {
            // 下载
            // 设置响应头
            response.setHeader("Cache-Control","max-age=0");
            // 设置响应类型为excel
            response.setContentType("application/vnd.mx-excel");
            // 设置文件头编码方式和文件名
            response.addHeader("Content-disposition",
                    "attachment;filename=" + new String(fileName.getBytes("gb2312"),"ISO-8859-1") + ".xls");
            OutputStream outputStream = response.getOutputStream();
            export.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 导入
    @PostMapping("auditoriums/import")
    @ResponseBody
    public boolean importAuditoriums(MultipartFile file){
        try {
            InputStream inputStream = file.getInputStream();
            List<Object> list = ExcelImportUtil.importExcel(inputStream, new Auditoriums());
            boolean flag = auditoriumsService.importExcel(list);
            return flag;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 下载模板
    @GetMapping("auditoriums/demo")
    public void getAuditoriumsDemoExcel(HttpServletRequest request,HttpServletResponse response){
        HSSFWorkbook hssfWorkbook = auditoriumsService.auditoriumsDemo();
        // 文件名
        String fileName = "影厅导入模板";
        try {
            // 下载
            // 设置响应头
            response.setHeader("Cache-Control","max-age=0");
            // 设置响应类型为excel
            response.setContentType("application/vnd.mx-excel");
            // 设置文件头编码方式和文件名
            response.addHeader("Content-disposition",
                    "attachment;filename=" + new String(fileName.getBytes("gb2312"),"ISO-8859-1") + ".xls");
            OutputStream outputStream = response.getOutputStream();
            hssfWorkbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
